package com.bwda.dsrs.systemservice.controller;


import com.bwda.dsrs.base.util.AesEncryptPaddingUtil;
import com.bwda.dsrs.base.util.ThreeDesUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.emailsettting.EmailContentCondition;
import com.bwda.dsrs.systemservice.domain.form.emailsettting.EmailSetttingCreateForm;
import com.bwda.dsrs.systemservice.domain.form.emailsettting.EmailSetttingQueryForm;
import com.bwda.dsrs.systemservice.domain.form.emailsettting.EmailSetttingUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.emailsettting.EmailSetttingPo;
import com.bwda.dsrs.systemservice.domain.condition.emailsettting.EmailSetttingCondition;
import com.bwda.dsrs.systemservice.domain.vo.emailsettting.EmailSetttingVo;
import com.bwda.dsrs.systemservice.service.EmailSetttingService;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;

import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.service.IMailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户菜单管理服务类
 *
 * @author yj
 * @date 2019年4月28日
 */
@RestController
@RequestMapping("/s/emailsettting")
@Api(value = "/s/emailsettting", tags = "邮件服务器配置表")
public class EmailSetttingController extends BaseController {

	@Autowired
	private EmailSetttingService emailSetttingService;

	@Autowired
	private IMailService iMailServicel;

	@Value("${aes_key}")
	private String aeskey;


	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件服务器配置表数量",notes = "查询邮件服务器配置表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid EmailSetttingQueryForm form) throws SystemServiceException {
		EmailSetttingCondition condition = this.getConditionByQueryForm(form);
		int count = emailSetttingService.queryCount(condition);
		return getSuccessResult(count);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件服务器配置表列表",notes = "查询邮件服务器配置表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<EmailSetttingVo>> queryList(@ModelAttribute@Valid EmailSetttingQueryForm form) throws SystemServiceException {
		EmailSetttingCondition condition = this.getConditionByQueryForm(form);
		List<EmailSetttingPo> poList = emailSetttingService.queryList(condition);
		List<EmailSetttingVo> voList = CopyUtil.transfer(poList, EmailSetttingVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件服务器配置表列表(带分页)",notes = "查询邮件服务器配置表列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询邮件服务器配置表列表(带分页)", type = LogTypeEnum.QUERY, include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<EmailSetttingVo>> queryPageList(@ModelAttribute@Valid EmailSetttingQueryForm form) throws SystemServiceException {
		EmailSetttingCondition condition = this.getConditionByQueryForm(form);
		List<EmailSetttingVo> voList = new ArrayList<>();
		condition.setDelFlag(Constants.ENABLE);
		int count = emailSetttingService.queryCount(condition);
		if (count > 0) {
			List<EmailSetttingPo> poList = emailSetttingService.queryList(condition);
			voList = CopyUtil.transfer(poList, EmailSetttingVo.class);
			voList.forEach(data -> {
				try {
					if (data.getPassword()!=null&&!"".equals(data.getPassword())){
						String pwd =ThreeDesUtil.decode(data.getPassword());
						data.setPassword(AesEncryptPaddingUtil.encrypt(pwd, aeskey));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "新增邮件服务器配置表",notes = "新增邮件服务器配置表",httpMethod = "POST")
	@Loggable(descp = "新增邮件服务器配置表", type = LogTypeEnum.CREATE, include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<EmailSetttingVo> add(@ModelAttribute@Valid EmailSetttingCreateForm form) throws SystemServiceException,Exception {
		EmailSetttingPo po = CopyUtil.transfer(form, EmailSetttingPo.class);
		if (po.getPassword()!=null&&!"".equals(po.getPassword())){
		po.setPassword(AesEncryptPaddingUtil.decrypt(po.getPassword(), aeskey));
		String encodePassPwd = ThreeDesUtil.encode(po.getPassword());
		po.setPassword(encodePassPwd);
		}
		emailSetttingService.insert(po);
		EmailSetttingVo vo = CopyUtil.transfer(po, EmailSetttingVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "修改邮件服务器配置表",notes = "修改邮件服务器配置表",httpMethod = "POST")
	@Loggable(descp = "修改邮件服务器配置表", type = LogTypeEnum.UPDATE, include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid EmailSetttingUpdateForm form) throws SystemServiceException,Exception {
		EmailSetttingPo po = CopyUtil.transfer(form, EmailSetttingPo.class);
		if (po.getPassword()!=null&&!"".equals(po.getPassword())){
			po.setPassword(AesEncryptPaddingUtil.decrypt(po.getPassword(), aeskey));
			String encodePassPwd = ThreeDesUtil.encode(po.getPassword());
			po.setPassword(encodePassPwd);
		}
		emailSetttingService.update(po);
		return getSuccessResult();
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "删除邮件服务器配置表",notes = "删除邮件服务器配置表",httpMethod = "POST")
	@Loggable(descp = "删除邮件服务器配置表", type = LogTypeEnum.DELETE, include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam List<Integer> id) throws SystemServiceException {
		if (id == null || id.size() == 0) {
			return getFailResult("参数错误");
		}
		emailSetttingService.batchDelete(id);
		return getSuccessResult();
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "测试邮件",notes = "测试邮件",httpMethod = "POST")
	@Loggable(descp = "测试邮件", type = LogTypeEnum.DELETE, include = "")
	@RequestMapping(value = "/testEmail", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity testEmail(@ModelAttribute@Valid EmailSetttingQueryForm form) throws SystemServiceException,Exception {
		EmailSetttingPo po =CopyUtil.transfer(form,EmailSetttingPo.class);
		if (po.getPassword()==null){
			throw  new SystemServiceException("缺少参数");
		}
		if (form.getTextEmail()==null){
			throw  new SystemServiceException("缺少测试邮箱");
		}
		try{
			po.setPassword(AesEncryptPaddingUtil.decrypt(form.getPassword(), aeskey));
			EmailContentCondition condition = new EmailContentCondition();
			condition.setSubject("测试");
			condition.setTo(form.getTextEmail());
			condition.setAttachmentList(new ArrayList<>());
			condition.setText("");
			iMailServicel.sendEmail(po,condition);
		}catch (Exception e){
			throw  new SystemServiceException("配置错误");
		}
		return getSuccessResult();
	}

	/**
	 * EmailSetttingQueryForm转换为EmailSetttingCondition
	 *
	 * @param form
	 * @return
	 */
	private EmailSetttingCondition getConditionByQueryForm(EmailSetttingQueryForm form) {
		EmailSetttingCondition condition = CopyUtil.transfer(form, EmailSetttingCondition.class);
		return condition;
	}

}